package com.woniu.cinema.service.impl;

import java.util.Date;
import java.util.List;

import com.woniu.cinema.domain.Movie;
import com.woniu.cinema.mapper.MovieMapper;
import com.woniu.cinema.vo.HallMovieVo;
import com.woniu.common.enums.ExceptionCode;
import com.woniu.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu.cinema.mapper.HallMovieMapper;
import com.woniu.cinema.domain.HallMovie;
import com.woniu.cinema.service.HallMovieService;

/**
 * 电影和影厅排片Service业务层处理
 * 
 * @author yy
 * @date 2022-03-30
 */
@Service
public class HallMovieServiceImpl implements HallMovieService
{
    @Autowired
    private HallMovieMapper hallMovieMapper;


    @Autowired
    private MovieMapper movieMapper;
    /**
     * 查询电影和影厅排片
     * 
     * @param hallMovieId 电影和影厅排片ID
     * @return 电影和影厅排片
     */
    @Override
    public HallMovie selectHallMovieById(Integer hallMovieId)
    {
        return hallMovieMapper.selectHallMovieById(hallMovieId);
    }

    /**
     * 查询电影和影厅排片列表
     * 
     * @param hallMovie 电影和影厅排片
     * @return 电影和影厅排片
     */
    @Override
    public List<HallMovie> selectHallMovieList(HallMovie hallMovie)
    {
        return hallMovieMapper.selectHallMovieList(hallMovie);
    }

    /**
     * 新增电影和影厅排片
     * 
     * @param hallMovie 电影和影厅排片
     * @return 结果
     */
    @Override
    public int insertHallMovie(HallMovie hallMovie)
    {
        //根据电影的放映时长，自动计算结束时间
        Movie movie = movieMapper.selectMovieById(hallMovie.getMovieId());
        Integer movieTime = movie.getMovieTime();
        hallMovie.setEndTime(new Date(hallMovie.getStartTime().getTime()+movieTime*60*1000));

        //检查排片计划是否有冲突
        //根据hallId查询所有的排片计划
        HallMovie hallMovie1 = new HallMovie();
        hallMovie1.setHallId(hallMovie.getHallId());
        List<HallMovie> hallMovies = hallMovieMapper.selectHallMovieList(hallMovie1);

        for (HallMovie hm : hallMovies) {
           if(!(hallMovie.getStartTime().getTime()>hm.getEndTime().getTime()
                   || hallMovie.getEndTime().getTime()<hm.getStartTime().getTime())){
               throw new CustomException(ExceptionCode.PLAY_TIME_CONFICTION);
           }
        }
        return hallMovieMapper.insertHallMovie(hallMovie);
    }

    /**
     * 修改电影和影厅排片
     * 
     * @param hallMovie 电影和影厅排片
     * @return 结果
     */
    @Override
    public int updateHallMovie(HallMovie hallMovie)
    {
        return hallMovieMapper.updateHallMovie(hallMovie);
    }

    /**
     * 批量删除电影和影厅排片
     * 
     * @param hallMovieIds 需要删除的电影和影厅排片ID
     * @return 结果
     */
    @Override
    public int deleteHallMovieByIds(Integer[] hallMovieIds)
    {
        return hallMovieMapper.deleteHallMovieByIds(hallMovieIds);
    }

    /**
     * 删除电影和影厅排片信息
     * 
     * @param hallMovieId 电影和影厅排片ID
     * @return 结果
     */
    @Override
    public int deleteHallMovieById(Integer hallMovieId)
    {
        return hallMovieMapper.deleteHallMovieById(hallMovieId);
    }

    @Override
    public List<HallMovieVo> queryHallMovieByMovieIdAndDate(Integer movieId,Integer cinemaId, String queryDate) {

        return hallMovieMapper.queryHallMovieByMovieIdAndDate(movieId,cinemaId,queryDate);


    }

    @Override
    public HallMovieVo queryHallMovieById(Integer hallMovieId) {
        return hallMovieMapper.queryHallMovieById(hallMovieId);


    }
}
